// pages/content/vipuser.js
var rsa = require('../../utils/rsa.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vipm: [{ title: '积分', amunt: '0' }, { title: '余额(元)', amunt: '0' }, { title: '优惠券(张)', amunt: '0' }],
    huyflag: false,    //到店
    chongzflag: false,    //充值
    linquanflag: false,  //领券

    seleIndex: 0,  //会员卡index
    couponIndex: 0, //优惠券index

    tel_input: '',
    vipArr_title: '', //会员到店提示
    vipdetail: {}, //vip详情

    vipmoneys:[], //充值面额
    couponArr:[], //门店优惠券
    noticet:'', //领取优惠券提示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  quxiad:function(){
    const detail = this.data.vipdetail;
    var tele = detail.mobile;
    wx.redirectTo({
      url: '/pages/content/payment?tel='+tele,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  //会员到店手机号
  inpaction: function (e) {
    this.setData({
      tel_input: e.detail.value,
      vipArr_title: '',
    })
  },
  //会员到店确定
  userArrive: function (e) {
    const tel = this.data.tel_input;
    if (!tel) {
      this.setData({
        vipArr_title: '手机号不为空',
      })
    } else {
      this.setData({
        vipArr_title: '处理中...',
      })
      var shopnum = app.globalData.shoopnum;
      const setion = app.globalData.session;
      wx.showLoading({
        title: '请求中...',
        mask: true,
      })
      rsa.Cpic_fetchPost(rsa.SERVERPORT, {
        'method': 'promotion.member_regist', "seller_bn": shopnum, "session_id": setion, "mobile": tel
      }, (err, res) => {
        wx.hideLoading();
        if (err) {
          this.setData({
            vipArr_title: err,
          })
          return;
        }
        var vipdet = this.data.vipm;
        vipdet[0].amunt = res.data.point;
        vipdet[1].amunt = res.data.advance;
        vipdet[2].amunt = res.data.coupon_list.length;
        this.setData({
          vipdetail: res.data, //vip详情
          vipm: vipdet,
          vipArr_title: res.return_msg,
          huyflag: false,
        })
      })
    }

  },
  //会员到店
  viparrive: function () {
    this.setData({
      huyflag: true,
    })
  },
  //选择哪个卡券
  selectCoupon: function (e) {
    const index = e.currentTarget.dataset.info;
    this.setData({
      couponIndex: index,
      noticet:'',
    })
  },
  //选择哪个充值金额
  selectwitch: function (e) {
    const index = e.currentTarget.dataset.info;
    this.setData({
      seleIndex: index,
    })
  },
  //隐藏
  hidedaod: function (e) {
    const index = e.currentTarget.dataset.info;
    switch (index) {
      case '1':
        this.setData({
          huyflag: false,
        })
        break;
      case '2':
        this.setData({
          chongzflag: false,
        })
        break;
      case '3':
        this.setData({
          linquanflag: false,
        })
        break;
    }
    this.setData({
      vipArr_title: '',
      tel_input: '',
    })

  },

  //会员lingq
  hylqaction: function () {
    const vip = this.data.vipdetail;
    if (vip.mobile) {
      this.checkCouponArray();
    }else{
      this.setData({
        huyflag: true,
      })
    }
  },
  //查询优惠券列表
  checkCouponArray:function(){
    var shopnum = app.globalData.shoopnum;
    const setion = app.globalData.session;
    wx.showLoading({
      title: '请求中...',
      mask: true,
    })
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'store.coupons', "seller_bn": shopnum, "session_id": setion
    }, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showModal({
          title: '提示',
          content: err,
          showCancel: false,
        })
        return;
      }
      var vipdet = res.data.list;
      if (vipdet.length == 0) {
        wx.showModal({
          title: '提示',
          content: '后台查询优惠券为空',
          showCancel: false,
        })
        return;
      }
      this.setData({
        couponArr: vipdet, //优惠券列表
        linquanflag: true,
      })
    })
  },

  //优惠券派送
  couponPaisong:function(){
    this.setData({
      noticet: '',
    })
    const index = this.data.couponIndex;
    const quanarr = this.data.couponArr;
    if (quanarr.length == 0) {
      return;
    }
    var mnsag = quanarr[index];
    var rcode = mnsag.cid; //优惠券ID
    var shopnum = app.globalData.shoopnum;
    const setion = app.globalData.session;
    var memberid = this.data.vipdetail.member_id; //会员id
    wx.showLoading({
      title: '请求中...',
      mask: true,
    })
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'coupon.provide', "seller_bn": shopnum, "session_id": setion, 'member_id': memberid, 'cid': rcode}, (err, res) => {
      wx.hideLoading();
      if (err) {
        this.setData({
          noticet:err,
        })
        return;
      }
      var vipdet = this.data.vipm;
      var details = parseInt(vipdet[2].amunt) +1; //vip详情
      vipdet[2].amunt = details;
      this.setData({
        noticet:'领取成功!',
        vipm: vipdet,
      })
    })



  },
  //会员充值
  hyczAction: function () {
    const vip = this.data.vipdetail;
    if (vip.mobile) {
      this.checkvipinmoney();
    }else{
      this.setData({
        huyflag: true,
      })
    }

  },
  //充值确定
  inpmoneyAction:function(){
    const index = this.data.seleIndex;
    const miner = this.data.vipmoneys;
    if(miner.length ==0){
      return;
    }
    var mnsag = miner[index];
    var rcode = mnsag.code; //充值码
    var shopnum = app.globalData.shoopnum;
    const setion = app.globalData.session;
    var memberid = this.data.vipdetail.member_id; //会员id
    wx.showLoading({
      title: '请求中...',
      mask: true,
    })
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'promotion.recharge', "seller_bn": shopnum, "session_id": setion, 'member_id': memberid, 'code': rcode, 'memo':'小程序会员充值'}, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showModal({
          title: '提示',
          content: err,
          showCancel: false,
        })
        return;
      }
      var oderid = res.data.order_id;
      if (oderid == '') {
        wx.showModal({
          title: '提示',
          content: '下单失败',
          showCancel: false,
        })
        return;
      }
      wx.navigateTo({
        url: '/pages/content/maxpay?ordeid=' + oderid,
      })
    })

  },
    //向后台查询充值类型
  checkvipinmoney:function(){
    var shopnum = app.globalData.shoopnum;
    const setion = app.globalData.session;
    wx.showLoading({
      title: '请求中...',
      mask: true,
    })
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'promotion.rechargeface', "seller_bn": shopnum, "session_id": setion}, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showModal({
          title: '提示',
          content: err,
          showCancel:false,
        })
        return;
      }
      var vipdet = res.data.list;
      if(vipdet.length ==0){
        wx.showModal({
          title: '提示',
          content: '后台查询充值面额为空',
          showCancel: false,
        })
        return;
      }
      this.setData({
        vipmoneys: vipdet, //充值面额
        chongzflag: true,
      })
    })
  
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})